<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\ShopResource;
use App\Models\Order;
use App\Models\Shop;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class OrdersController extends Controller
{
    public function orders(Order $order,Request $request)
    {
        $user = Auth::guard('api')->user();
        $price_array=[];
        foreach (array_keys(config("price")) as $key){
            array_push($price_array,substr($key,0,1));
        };
//        dd($price_array,[1,2],$request->length,Rule::in([1,2]));
        $validator = Validator::make($request->all(), [
            'shop_id' => 'required|numeric|exists:App\Models\Shop,id',
            'room_id' => 'required|numeric|exists:App\Models\Room,id',
            'length' => 'required|numeric|in:'.implode(',',$price_array),
            'dev_id' => 'required|numeric|exists:App\Models\Device,id',
            'yuyue_at' => 'required|date_format:Y-m-d H:i:s',
        ],[
            'required'=>':attribute是必须的',
            'numeric'=>':attribute必须是数字',
            'exists'=>':attribute 不存在',
            'in'=>'该时长不存在',
            'date_format'=>'时间格式错误',
        ]);
        if($validator->fails()){
            foreach ($validator->errors()->messages() as $message){
                return $this->error($message[0]);
            }
        }
        $length=$request->length.'h';
        $price=config("price")[$length];
        $query=$order->newQuery();
        Cache::put($user->id,$request->all(),20);
        $cahe=Cache::get($user->id);
        dd($cahe);

    }
}
